package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.*;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.entity.vo.CourseListQuery;
import com.atguigu.eduservice.entity.vo.CoursePublish;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.handler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.nio.file.Watchable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author zhaolie
 * @since 2020-12-08
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Resource
    private EduCourseDescriptionService courseDescriptionService;

    @Resource
    private EduChapterService chapterService;

    @Resource
    private EduVideoService videoService;

    @Override
    public String saveCourseInfo(CourseQuery courseQuery) {
        // 1.向课程表添加课程基本信息(courseQuery转换eduCourse)
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseQuery, eduCourse);
        int i = baseMapper.insert(eduCourse);
        if (i == 0) {
            throw new GuliException(20001, "添加课程信息失败");
        }
        // 2.向课程简介表添加课程简介
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setId(eduCourse.getId());
        courseDescription.setDescription(courseQuery.getDescription());
        courseDescriptionService.save(courseDescription);
        return eduCourse.getId();
    }

    @Override
    public CourseQuery getCourseInfo(String courseId) {
        // 1.查询课程信息
        EduCourse course = baseMapper.selectById(courseId);
        // 2.查询描述
        EduCourseDescription description = courseDescriptionService.getById(courseId);
        CourseQuery courseQuery = new CourseQuery();
        BeanUtils.copyProperties(course, courseQuery);
        BeanUtils.copyProperties(description, courseQuery);
        return courseQuery;
    }

    @Override
    public void updateCourseInfo(CourseQuery courseQuery) {
        // 1.修改课程
        EduCourse course = new EduCourse();
        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseQuery, course);
        BeanUtils.copyProperties(courseQuery, description);
        int i = baseMapper.updateById(course);
        if (i == 0) {
            throw new GuliException(20001, "修改课程信息失败");
        }
        courseDescriptionService.updateById(description);
    }

    @Override
    public CoursePublish publishCourseInfo(String courseId) {
        CoursePublish courseInfo = baseMapper.getPublishCourseInfo(courseId);
        return courseInfo;
    }

    @Override
    public void publishCourse(String courseId) {
        EduCourse course = new EduCourse();
        course.setId(courseId);
        course.setStatus("Normal");
        baseMapper.updateById(course);
    }

    @Override
    public Map pageCourseCondition(Long current, Long limit, CourseListQuery courseListQuery) {
        Page<EduCourse> coursePage = new Page<>(current, limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        String title = courseListQuery.getTitle();
        String status = courseListQuery.getStatus();
        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        wrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(coursePage, wrapper);
        Long total = coursePage.getTotal(); // 总记录数
        List<EduCourse> records = coursePage.getRecords(); // 每页数据
        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return map;
    }

    @Override
    public void deleteCourse(String courseId) {
        baseMapper.deleteById(courseId);
        courseDescriptionService.removeById(courseId);
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        chapterService.remove(chapterQueryWrapper);
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id", courseId);
        videoService.remove(videoQueryWrapper);
    }

    @Override
    public Map<String, Object> getCourseList(CourseFrontVo courseFrontVo, Long current, Long limit) {
        Page<EduCourse> coursePage = new Page<>(current, limit);
        QueryWrapper<EduCourse> courseQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) {
            courseQueryWrapper.eq("subject_parent_id", courseFrontVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())) {
            courseQueryWrapper.eq("subject_id", courseFrontVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) {
            courseQueryWrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {
            courseQueryWrapper.orderByDesc("price");
        }
        courseQueryWrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(coursePage, courseQueryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("rows", coursePage.getRecords());
        map.put("current", coursePage.getCurrent());
        map.put("pages", coursePage.getPages());
        map.put("size", coursePage.getSize());
        map.put("total", coursePage.getTotal());
        map.put("hasNext", coursePage.hasNext());
        map.put("hasPrevious", coursePage.hasPrevious());

        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        CourseWebVo courseWebVo = baseMapper.getBaseCourseInfo(courseId);
        return courseWebVo;
    }
}
